package com.example.lanouhn.firstapp.activity.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.model.CartList;
import com.example.lanouhn.firstapp.activity.model.CartListEdit;
import com.example.lanouhn.firstapp.activity.model.Syomb;
import com.example.lanouhn.firstapp.activity.utils.SPUtils;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 订单列表数据适配器
 * <p/>
 *
 */
public class MyOrderAdapter extends RecyclerView.Adapter {

    //上下文对象
    private Context context;
    //数据集合
    private List<CartList> list;

    private String key;
    private Syomb slist;
    private CartListEdit cList;

    public MyOrderAdapter(Context context, List<CartList> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载布局
        RecyclerView.ViewHolder holder = null;

        holder = new VHolder(
                LayoutInflater.from(context).

                        inflate(R.layout.item_order_list, null));
        //获取sharedPreference中存储的key
        key = (String) SPUtils.get(context, "log_key", "");
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {


        final VHolder vHolder = (VHolder) holder;
        //数据位置
        final CartList cartList = list.get(position);
        //设置数据 运用picasso获取图片
        Picasso.with(context).load(cartList.getGoods_image_url()).into(vHolder.goods_image_url);
        vHolder.goods_name.setText(cartList.getGoods_name());
        vHolder.goods_sum.setText(cartList.getGoods_sum());
        vHolder.goods_num.setText(cartList.getGoods_num());
        vHolder.goods_price.setText(cartList.getGoods_price());

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strTime = format.format(new Date());
        vHolder.date.setText(strTime);


    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    //定义VHolder类继承RecyclerView.ViewHolder
    class VHolder extends RecyclerView.ViewHolder {


        private TextView goods_name;
        private TextView goods_price;
        private TextView goods_num;
        private ImageView goods_image_url;
        private TextView goods_sum;

        private TextView date;
        //绑定控件id
        public VHolder(View itemView) {
            super(itemView);
            goods_image_url = (ImageView) itemView.findViewById(R.id.goods_image_url);
            goods_name = (TextView) itemView.findViewById(R.id.goods_name);
            goods_price = (TextView) itemView.findViewById(R.id.goods_price);
            goods_num = (TextView) itemView.findViewById(R.id.goods_num);
            goods_sum = (TextView) itemView.findViewById(R.id.tv_price);
            //下单时间
            date = (TextView) itemView.findViewById(R.id.date);

        }
    }

}
